$(function() {
	// 创建富文本编辑器
	itemAddEditor = TAOTAO.createEditor("#itemAddForm [name=desc]");
	// 初始化类目选择和图片上传器
	TAOTAO.init({
		fun : function(node) {
			// 根据商品的分类id取商品 的规格模板，生成规格信息。第四天内容。
			TAOTAO.changeItemParam(node, "itemAddForm");
		}
	});
});
var statusvalue = [ {
	statusId : '1',
	name : '正常'
}, {
	statusId : '2',
	name : '删除'
} ]
var isParentvalue = [ {
	isParentId : true,
	name : '是'
}, {
	isParentId : false,
	name : '否'
} ]
function formatProgress(value) {
	var s = '<a href="javascript:void(0)" onClick="uploadpic()">上传图片</a><input type="hidden" name="photoX"/>'
			+ '<a href="javascript:void(0)" onClick="deletepic('
			+ value
			+ ')">删除图片</a>';
	return s;
}
function formatImages(value) {
	if (value) {
		var s = '<a href="' + value + '"><img src="' + value
				+ '" style="width: 15px;height: 15px;"></a>';
		return s;
	} else {
		return '';
	}

}
function uploadpic() {
	$("#itemCatEditWindow").window({
		onLoad : function() {
			// 回显数据
			var data = $("#tg").datagrid("getSelections")[0];
			$("#itemeCatEditForm").form("load", data);
			TAOTAO.init({
				"pics" : data.photoX,
				"cid" : data.cid,
				fun : function(node) {
					TAOTAO.changeItemParam(node, "itemeCatEditForm");
				},
				form : 'catadd'
			});
		}
	}).window("open");
}
function deletepic(value) {
	$.ajax({
		url : './item/cat/update',
		method : 'post',
		data : {
			'id' : value,
			'photoX' : ''
		},
		success : function() {
			alert("图标删除成功")
			$('#tg').treegrid('reload');
		},
		error : function() {
			alert("重新添加，添加失败")
		}
	})
}
function onContextMenu(e, row) {
	e.preventDefault();
	$(this).treegrid('select', row.id);
	$('#memberchilendtool').menu('show', {
		left : e.pageX,
		top : e.pageY
	});
}
var idIndex = 100;
function append() {
	idIndex++;
	var d1 = new Date();
	var d2 = new Date();
	d2.setMonth(d2.getMonth() + 1);
	var node = $('#member').treegrid('getSelected');
	$.ajax({
		url : './member/add',
		method : 'post',
		data : {
			"parentId" : node.id,
			"nickname" : '我的一个会员',
			"sortOrder" : 0
		},
		success : function(res) {
			$('#tg').treegrid('append', {
				parent : node.id,
				data : [ {
					id : res.data.id,
					text : res.data.name,
					status : res.data.status == 1 ? '正常' : '删除',
					sortOrde : res.data.sortOrde,
					isParent : res.data.isParent ? '是' : '否',
				} ]
			})
		},
		error : function() {
			alert("重新添加，添加失败")
		}
	})

}
function removeIt() {
	var node = $('#tg').treegrid('getSelected');
	if (node) {
		$('#tg').treegrid('remove', node.id);
	}
	$.ajax({
		url : './item/cat/delete',
		method : 'post',
		data : {
			"id" : node.id
		},
		success : function(res) {
			alert("成功")
			$('#tg').treegrid('reload');
		},
		error : function() {
			alert("重新添加，操作失败")
		}
	})
}
function collapse() {
	var node = $('#tg').treegrid('getSelected');
	if (node) {
		$('#tg').treegrid('collapse', node.id);
	}
}
function expand() {
	var node = $('#tg').treegrid('getSelected');
	if (node) {
		$('#tg').treegrid('expand', node.id);
	}
}
var editingId;
function edit() {
	if (editingId != undefined) {
		$('#tg').treegrid('select', editingId);
		return;
	}
	var row = $('#tg').treegrid('getSelected');
	if (row) {
		editingId = row.id
		$('#tg').treegrid('beginEdit', editingId);
	}
	var editors = $('#tg').datagrid('getEditors', editingId);
	for (var i = 0, len = editors.length; i < len; i++) {
		var editor = editors[i];
		$(editor.target).bind('keyup', function(e) {
			var code = e.keyCode || e.which;
			if (code == 13) {
				save()
			}
		});
	}
}
function save() {
	if (editingId != undefined) {
		var t = $('#tg');
		t.treegrid('endEdit', editingId);
		var node = t.treegrid("find", editingId)
		node.isParent = node.isParent == '是' ? true : false
		node.status = node.status == '正常' ? 1 : 0
		node.name = node.text
		$.ajax({
			url : './item/cat/update',
			method : 'post',
			data : node,
			success : function() {
				editingId = undefined;
			},
			error : function() {
				alert("重新添加，添加失败")
			}
		})

	}
}
function add() {
	var membernickname = $('#parentId').combobox('getText');
	var membercode = $("#parentId").combobox('getValue');
	var membername = $('#membername').textbox('getValue');
	var memberno = $('#memberno').textbox('getValue');
	var password = $('#password').textbox('getValue');
	var tel = $('#tel').textbox('getValue');
	var job = $('#job').textbox('getValue');
	var sex = $('#sex').textbox('getValue');
	var provice = $('#provice').textbox('getValue');
	var city = $('#vipcode').textbox('getValue');
	var vipcode = $('#vipcode').textbox('getValue');
	var level = $('#level').textbox('getValue');
	$.ajax({
		url : './member/add',
		method : 'post',
		data : {
			"parentId" : membercode,
			"membername" : membername,
			"memberno":memberno,
			"password" : password,
			"tel" : tel,
			"job" : job,
			"sex" : sex,
			"provice" : provice,
			"city":city,
			"vipcode" : vipcode,
			"level" : level
			},
		success : function() {
			$('#addmember').window('close')
			$('#member').treegrid('reload');
		},
		error : function() {
			alert("重新添加，添加失败")
		}
	})

}
function tojiesuan(value, rowData) {
	if (value == "未结算" && rowData.isParent == "是") {
		return value + " <button onclick='tojiesuanyongjin(" + rowData.id
				+ ")'>结算</button>";
	} else {
		return value;
	}

}
function tojiesuanyongjin(id) {
	$.ajax({
		url : './member/jiesuan',
		method : 'post',
		data : {
			"id" : id
		},
		success : function() {
			$('#member').treegrid('reload');
		},
		error : function() {
			alert("结算失败")
		}
	})
}